package com.ruoyi.system.service.impl;

import com.ruoyi.common.snmp.getSnmp.SNMPSessionUtil;
import com.ruoyi.common.snmp.handler.SysncInterFlow;
import com.ruoyi.system.domain.SnmpInterflow;
import com.ruoyi.system.mapper.SnmpInterflowMapper;
import com.ruoyi.system.service.ISnmpInterflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@Service
public class SnmpInterflowServiceImpl implements ISnmpInterflowService
{
    @Autowired
    private SnmpInterflowMapper snmpInterflowMapper;
    @Autowired
    private SysncInterFlow sysncInterFlow;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SnmpInterflow selectSnmpInterflowById(Long id)
    {
        return snmpInterflowMapper.selectSnmpInterflowById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param snmpInterflow 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SnmpInterflow> selectSnmpInterflowList(SnmpInterflow snmpInterflow)
    {
        return snmpInterflowMapper.selectSnmpInterflowList(snmpInterflow);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param snmpInterflow 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSnmpInterflow(SnmpInterflow snmpInterflow)
    {
        return snmpInterflowMapper.insertSnmpInterflow(snmpInterflow);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param snmpInterflow 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSnmpInterflow(SnmpInterflow snmpInterflow)
    {
        return snmpInterflowMapper.updateSnmpInterflow(snmpInterflow);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSnmpInterflowByIds(Long[] ids)
    {
        return snmpInterflowMapper.deleteSnmpInterflowByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSnmpInterflowById(Long id)
    {
        return snmpInterflowMapper.deleteSnmpInterflowById(id);
    }

    @Override
    public List<SnmpInterflow> getFlowInfo(String ip) throws Exception{
        String[] oids = {".1.3.6.1.2.1.2.2.1.1"};//接口index
        String[] oids1 = {".1.3.6.1.2.1.2.2.1.2"};//接口描述
        List<SnmpInterflow> interFlows = new ArrayList<>();
        SnmpInterflow inter;
        SNMPSessionUtil snmp = new SNMPSessionUtil(ip, "161", "public", "2");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        ArrayList<String> indexs = snmp.snmpWalk2(oids);
        ArrayList<String> descs = snmp.snmpWalk2(oids1);
        CompletableFuture<List<List<String>>> interFlow = sysncInterFlow.getInterFlow(ip, 3000);//接口出入流量
        CompletableFuture<List<List<String>>> interFlow1 = sysncInterFlow.getinRate(ip, 3000);//接口出入利用率
        CompletableFuture<List<List<String>>> interFlow2 = sysncInterFlow.getUpLoss(ip, 3000);//接口出入丢包率
        CompletableFuture<List<List<String>>> interFlow3 = sysncInterFlow.getErrorRate(ip, 3000);//接口出入包错误率
        for (int i = 0; i < interFlow.get().get(0).size(); i++) {
            inter = new SnmpInterflow();
            inter.setIp(ip);
            inter.setInFlow(interFlow.get().get(0).get(i));
            inter.setOutFlow(interFlow.get().get(1).get(i));
            inter.setInRate(isASCII(interFlow1.get().get(0).get(i)));
            inter.setOutRate(isASCII(interFlow1.get().get(1).get(i)));
            inter.setInLoss(interFlow2.get().get(0).get(i));
            inter.setOutLoss(interFlow2.get().get(1).get(i));
            inter.setInErrorRate(interFlow3.get().get(0).get(i));
            inter.setOutErrorRate(interFlow3.get().get(1).get(i));
            inter.setIfindex(indexs.get(i).substring(indexs.get(i).lastIndexOf("=")).replace("=", "").trim());
            inter.setInterDescr(descs.get(i).substring(descs.get(i).lastIndexOf("=")).replace("=", "").trim());
            inter.setTime(df.format(new Date()));
            snmpInterflowMapper.insertSnmpInterflow(inter);
        }
        return interFlows;
    }
    @Override
    public List<SnmpInterflow>  getFlowByIp(String ip){
        return snmpInterflowMapper.getFlowByIp(ip);
    }

    public String isASCII(String value)
    {
        StringBuilder validChars = new StringBuilder();
        for (char c : value.toCharArray()) {
            if (c >= 32 && c <= 126) { // ASCII 可打印字符范围
                validChars.append(c);
            }
        }
        return validChars.toString();
    }
}
